package com.user.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.user.dao.dish.CreatOrUpdateDish;
import com.user.dao.dish.QueryPageDish;
import com.user.domain.Dish;
import com.user.mapper.DishMapper;
import com.user.service.DishService;
import com.user.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author YU
 * @Description 针对表【dish】的数据库操作Service实现
 * @Date 2023-12-29 11:09:06
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    DishMapper dishMapper;


    @Override
    @Transactional(rollbackFor = {java.lang.Exception.class})
    public R saveOrUpdateDish(CreatOrUpdateDish dishData) {
        Dish dish;
        if (dishData.getId() == null) {
            // Create new dish
            if (isDishNameExist(dishData.getName(), dishData.getRestaurantId())) return R.error("菜品名称已存在");
            dish = new Dish();
        } else {
            // Update existing dish
            dish = dishMapper.selectById(dishData.getId());
            if (dish == null) return R.error("菜品不存在");
            if (!dish.getName().equals(dishData.getName()) && isDishNameExist(dishData.getName(), dishData.getRestaurantId()))
                return R.error("菜品名称已存在");
        }
        BeanUtils.copyProperties(dishData, dish);
        dish.setRestaurantId(dishData.getRestaurantId());
        boolean result = saveOrUpdate(dish);
        return result ? R.ok() : R.error();
    }


    @Override
    public R getPageDish(QueryPageDish dish) {
        dish.setRestaurantId(dish.getRestaurantId());
        Page<Dish> mapPage = new Page<>(dish.getCurrent(), dish.getSize(), false);
        IPage<Dish> page = dishMapper.selectDish(dish, mapPage);
        page.setTotal(dishMapper.selectDishCount(dish));
        return R.ok().setData(page);
    }

    @Override
    public R getDishById(List<Integer> ids) {
        if (ids.isEmpty()) return R.ok();
        try {
            List<Dish> dish = dishMapper.selectBatchIds(ids);
            return R.ok().setData(dish);
        } catch (Exception e) {
            DishServiceImpl.log.error("getDishById error: {}", e.getMessage());
            return R.error();
        }
    }

    @Override
    public R deleteDishById(Integer id) {
        int i = dishMapper.deleteById(id);
        return i > 0 ? R.ok() : R.error();
    }

    /**
     * 判断菜品名称是否存在
     *
     * @param name         菜品名称
     * @param restaurantId
     * @return boolean
     */
    private boolean isDishNameExist(String name, Integer restaurantId) {
        Integer count = dishMapper.selectDishNameCount(name, restaurantId);
        return count > 0;
    }

    @Override
    public R getDishPhoto(Integer id) {
        String path = dishMapper.selectPhotoById(id);
        if (path != null && !path.isEmpty()) return R.ok().setData(path);
        return R.error("菜品图片不存在");
    }
}






